package com.syb.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import mybatis.com.syb.dao.MyBatisResourcesDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.syb.dao.UserDao;
import com.syb.entity.user.SysUser;
import com.syb.service.UserService;
import com.syb.util.PageResults;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private MyBatisResourcesDao myUserDao;

	@Override
	public PageResults<SysUser> getUserList(PageResults<SysUser> page)throws Exception {
		List<SysUser> userList = new ArrayList<SysUser>();
		PageResults<SysUser>  pageList = new PageResults<SysUser>();
		int count = 0;
		try{
			userList = userDao.findUserList(page);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		try{
			count = userDao.findUsercount(page);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		pageList.setTotalCount(count);
		pageList.setPageNo((page.getCurrentPage()/page.getPageSize())+1);
		pageList.setResults(userList);
		
		return pageList;
	}

	@Override
	public void saveSysUser(SysUser sysUser) throws Exception {
		SysUser sysUs = null;
		try{
			sysUs = userDao.findUserByName(sysUser.getSysName());
		}catch(Exception e){
			throw new Exception("");
		}
		if(sysUs!=null){
			throw new Exception("用户已 存在！");
		}
		try{
			userDao.save(sysUser);
		}catch(Exception e){
			throw new Exception("添加失败！");
		}
	}

	@Override
	public void updateSysUser(SysUser sysUser) throws Exception {
		SysUser user = null;
		try{
			user = userDao.findUserById(sysUser.getId());
		}catch(Exception e){
			throw new Exception("根据Id查询失败！");
		}
		if(user==null){
			throw new Exception("更新的数据不存在！");
		}
		if(!sysUser.getSysName().equals(user.getSysName())){
			try{
				user = userDao.findUserByName(sysUser.getSysName());
			}catch(Exception e){
				throw new Exception("根据名称查询失败！");
			}
			if(user!=null){
				throw new Exception("名称已存在！");
			}
		}
		try{
			myUserDao.updateUser(sysUser);
		}catch(Exception e){
			throw new Exception("更新失败！");
		}
	}

	@Override
	public void deleteSysUser(SysUser sysUser) throws Exception {
		//删除角色和用户的关联
		try{
			myUserDao.deleteRelRoleUserById(sysUser.getId());
		}catch(Exception e){
			throw new RuntimeException("删除关联关系失败！");
		}
		//删除角色
		try{
			userDao.delete(sysUser);
		}catch(Exception e){
			throw new RuntimeException("删除失败！");
		}
	}

	@Override
	public SysUser tzUpdateUser(Integer id) throws Exception {
		SysUser sys = new SysUser();
		try{
			sys = userDao.findUserById(id);
		}catch(Exception e){
			throw new Exception("查询失败!");
		}
		return sys;
	}

	@Override
	public List<Map<String, Object>> findRoleList(Integer id) throws Exception {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = myUserDao.findRoleList(id);
		}catch(Exception e){
			throw new Exception("查询失败!");
		}
		return list;
	}

	@Override
	public void updateRoleList(List<Map<String, Object>> list) throws Exception {
		//删除关联表
		if(list.size()>0){
			Integer	id = Integer.parseInt(list.get(0).get("id").toString());
			try{
				myUserDao.delRoleList(id);
			}catch(Exception e){
				throw new RuntimeException("删除失败！");
			}
		}
		if(list.get(0).containsKey("roleId")){
			try{
				myUserDao.insertRoleList(list);
			}catch(Exception e){
				throw new RuntimeException("更新失败！");
			}
		}
	}

}
